package org.shipp.util;

import org.shipp.activity.PrincipalActivity;
import org.shipp.activity.R;

import android.content.Context;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.RelativeLayout;
/**
 * 
 * @author Efren
 *
 */
public abstract class OnSwipeTouchListener implements OnTouchListener {

	private int touchIni;
	private int touchFim;
	private float toqueAnterior;
	private long tempoIni;
	private long tempoFim;
	private boolean ehDireita;
	private boolean permiteMoverSoEsquerda = true;
	private RelativeLayout layout; 
	private int tamMaximo;

	public OnSwipeTouchListener(Context c, RelativeLayout layout) {
		Display display = ((WindowManager) c.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		tamMaximo = (int) (display.getWidth() * PrincipalActivity.INDICE_PORCENTAGEM_TELA_PADRAO);
		
		this.layout = layout;
	}

	public boolean onTouch(final View viewClicada, final MotionEvent event) {
		final int X = (int) event.getRawX();
		RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) layout.getLayoutParams();
		tempoFim = System.currentTimeMillis();
		switch (event.getAction() & MotionEvent.ACTION_MASK) {
		case MotionEvent.ACTION_DOWN:
			touchIni = X;
			tempoIni = System.currentTimeMillis();
			toqueAnterior = event.getRawX();
			ehDireita = true;
			break;
		case MotionEvent.ACTION_UP:
			touchFim = X;
			if (ehDireita) {
				long tempoTotal = tempoFim - tempoIni;
				float dist = Math.abs(X - touchIni);
				double tempoPorDistancia = (double) (tempoTotal / dist);
				int oQueSobraTamanho = (int) (tamMaximo - dist);
				double resultTempoQueSobra = Math.abs(oQueSobraTamanho * tempoPorDistancia);

				/**
				 * Cond 1 - o cara ta deslizando pra direita
				 * Cond 2 - o cara ta deslizando pra esquerda
				 * Cond 3 - toque inicial menor que o tamanho maximo de scroll
				 */
				if (((lp.leftMargin + (X - toqueAnterior)) > 0 && (lp.leftMargin + (X - toqueAnterior)) < tamMaximo) || (touchIni < tamMaximo) || touchIni > (tamMaximo /2)) {
					if (touchFim > touchIni) {
						onSwipeRight(lp.leftMargin, (long) resultTempoQueSobra);
					} else {
						onSwipeLeft(lp.leftMargin, (long) resultTempoQueSobra);
					}
					lp.leftMargin = PrincipalActivity.RECUO_PADRAO;
					layout.setLayoutParams(lp);
				}
			}
			break;
		case MotionEvent.ACTION_POINTER_DOWN:
			break;
		case MotionEvent.ACTION_POINTER_UP:
			break;
		case MotionEvent.ACTION_MOVE:
			int lm = lp.leftMargin;
			if ((event.getRawX() < touchIni || !permiteMoverSoEsquerda) && (event.getRawX() < tamMaximo || touchIni < tamMaximo)) {
				if ((lm + (X - toqueAnterior)) > 0 && (lm + (X - toqueAnterior)) < tamMaximo) {
					int marginAtual = (int) (lm + (X - toqueAnterior));
					lp.leftMargin = marginAtual;
					layout.setLayoutParams(lp);
					
	            	for(int i=0; i<((ViewGroup)layout).getChildCount(); ++i) {
	            		View nextChild = ((ViewGroup)layout).getChildAt(i);
	            		if(nextChild.getId() == R.id.web){
	            			int rm = marginAtual < 0 ? 0 : marginAtual;
	            			RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(nextChild.getLayoutParams());
	            			params2.setMargins(0, 0, (rm * -1), 0);
	            			params2.addRule(RelativeLayout.BELOW, R.id.actionBar);
	            			nextChild.setLayoutParams(params2);
	            			Log.i("Move", marginAtual + " - " + lm + " - " + rm + " - " + toqueAnterior + " - " + X);
	            		}

	            	}
	            	toqueAnterior = X;
					
				}
				ehDireita = true;
			} else {
				ehDireita = false;
			}
			break;
		}
		return true;
	}

	public abstract void onSwipeRight(float ondeEsta, long tempo);

	public abstract void onSwipeLeft(float ondeEsta, long tempo);

}